/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jtsmix.mixfile;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * @author halecivo
 */
public class MixEncryption {

    private SecretKeySpec cryptoKey;
    private static final String BLOWFISH_CIPHER_INSTANCE = "Blowfish/ECB/NoPadding";

    public MixEncryption(byte[] cryptoKey) {
        // copy is made to avoid external modification
        byte cryptoKeyCopy[] = Arrays.copyOf(cryptoKey, cryptoKey.length);
        this.cryptoKey = new SecretKeySpec(cryptoKeyCopy, "Blowfish");
    }

    public static byte[] encryptBlowfish(byte[] toEncrypt, byte[] keyBytes) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "Blowfish");
            Cipher cipher = Cipher.getInstance(BLOWFISH_CIPHER_INSTANCE);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return cipher.doFinal(toEncrypt);
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] decryptBlowfish(byte[] toDecrypt, byte[] keyBytes) {
        try {
            SecretKeySpec key = new SecretKeySpec(keyBytes, "Blowfish");
            Cipher cipher = Cipher.getInstance(BLOWFISH_CIPHER_INSTANCE);
            cipher.init(Cipher.DECRYPT_MODE, key);
            return cipher.doFinal(toDecrypt);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(MixEncryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(MixEncryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(MixEncryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(MixEncryption.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(MixEncryption.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public byte[] encryptBlowfish(byte[] toEncrypt) {
        try {
            Cipher cipher = Cipher.getInstance(BLOWFISH_CIPHER_INSTANCE);
            cipher.init(Cipher.ENCRYPT_MODE, cryptoKey);
            return cipher.doFinal(toEncrypt);
        } catch (Exception e) {
            return null;
        }
    }

    public byte[] decryptBlowfish(byte[] toDecrypt) {
        try {
            Cipher cipher = Cipher.getInstance(BLOWFISH_CIPHER_INSTANCE);
            cipher.init(Cipher.DECRYPT_MODE, cryptoKey);
            return cipher.doFinal(toDecrypt);
        } catch (Exception e) {
            return null;
        }
    }


}
